package com.content;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

@Controller
public class ContentController {
    /**
     * 项目内文件
     * @return
     */
    @RequestMapping("/videos")
    public String videos(){
        return "videos.html";
    }

    /**
     * 本地磁盘文件
     * @return
     */
    @RequestMapping("/videosLocal")
    public String videosLocal(){
        return "videosLocal.html";
    }

    @RequestMapping("/playVideo")
    public String playVideo(Model model,@RequestParam String fileName){
        model.addAttribute("fileName",fileName);
        return "videoPlay.html";
    }
    /**
     * 本地磁盘取文件
     * @return
     */
    @Value("${cbs.fileListPath}")
    private String fileListPath;
    @Value("${cbs.viedosPath}")
    private String viedosPath;
    @RequestMapping("/fileListLocal")
    @ResponseBody
    public Map fileListLocalJson(){
        Map fileMap = new HashMap();
        fileListPath = fileListPath.replaceAll("file:","");
        File fileD1 = new File(fileListPath);
        fileMap = constractFileMap(fileMap,fileD1);
        viedosPath = viedosPath.replaceAll("file:","");
        File fileD2 = new File(viedosPath);
        fileMap = constractFileMap(fileMap,fileD2);
        return  fileMap;
    }

    /**
     * 这方法暂时只支持单层目录结构，MP4文件看成一堆，其他文件一堆
     * @param fileMap
     * @param fileDir
     * @return
     */
    public Map constractFileMap(Map fileMap,File fileDir){
        Map mp4Map = new TreeMap(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o2.compareTo(o1);
            }
        });
        Map otherMap = new TreeMap(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o2.compareTo(o1);
            }
        });
        File[] listFiles = fileDir.listFiles();
        for (int i = 0; i < listFiles.length; i++) {
            String absolutePath = listFiles[i].getAbsolutePath();
            String fileName = listFiles[i].getName();
            if(null != absolutePath && absolutePath.length() > 0 && listFiles[i].isFile()){
                String suffix = absolutePath.substring(absolutePath.lastIndexOf("."));
                if(null != suffix){
                    if(suffix.toLowerCase().equals(".mp4")){
                        mp4Map.put(fileName,absolutePath);
                    }else{
                        otherMap.put(fileName,absolutePath);
                    }
                }
            }
        }
        /**
         * 原始map加入
         */
        Map mp4MapTemp = (Map)fileMap.get("mp4Map");
        if(null != mp4MapTemp){
            mp4Map.putAll(mp4MapTemp);
        }
        fileMap.put("mp4Map",mp4Map);
        Map otherMapTemp = (Map)fileMap.get("otherMap");
        if(null != otherMapTemp){
            otherMap.putAll(otherMapTemp);
        }
        fileMap.put("otherMap",otherMap);
        return  fileMap;
    }
    /**
     * 项目内取文件
     * @return
     */
    @RequestMapping("/fileList")
    @ResponseBody
    public Map listFilesJson(){
        Map fileMap = new HashMap();
        String path = null;
        try {
            path = ResourceUtils.getURL("classpath:").getPath();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if(null != path && path.length() > 0){
            File upload = new File(path,"static/upload/");
            if(!upload.exists()){
                upload.mkdirs();
            } else{
                fileMap = constractFileMap(fileMap,upload);
            }
        }
        return  fileMap;
    }
}
